package com.ruoyi.loan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.loan.constant.ExamineStatus;
import com.ruoyi.loan.constant.PoorGrade;
import com.ruoyi.loan.constant.PoorType;
import com.ruoyi.loan.dto.PoorApplyDto;
import com.ruoyi.loan.dto.SelectPoorApplyDto;
import com.ruoyi.loan.entity.PoorApplyRecord;
import com.ruoyi.loan.mapper.PoorApplyMapper;
import com.ruoyi.loan.po.SelectPoorApplyPo;
import com.ruoyi.loan.service.PoorApplyService;
import com.ruoyi.loan.vo.SelectPoorApplyVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lin
 * @Date 2022/4/5 0:18
 */
@Service
public class PoorApplyServiceImpl extends ServiceImpl<PoorApplyMapper, PoorApplyRecord> implements PoorApplyService {

    @Resource
    private PoorApplyMapper applyMapper;

    /**
     * 贫困生申请
     * @param dto 审核信息
     */
    @Override
    public void updatePoorApply(PoorApplyDto dto) {
        //获取管理员id
        SysUser sysUser = ShiroUtils.getSysUser();
        if (sysUser == null){
            throw new RuntimeException("请先登录");
        }
        Integer userId = Integer.valueOf(String.valueOf(sysUser.getUserId()));
        //获取审核时间
        long current = System.currentTimeMillis();
        Timestamp timestamp = new Timestamp(current);
        PoorApplyRecord applyRecord = new PoorApplyRecord();
        applyRecord.setAdminId(userId);
        applyRecord.setAdminTime(timestamp);
        applyRecord.setStatus(ExamineStatus.SUCCESS.getCode());
        applyRecord.setResultRank(dto.getResultRank());
        applyRecord.setId(dto.getId());
        baseMapper.updateById(applyRecord);
    }

    /**
     * 获取贫困生申请列表
     * @param dto 条件信息
     * @return 申请列表
     */
    @Override
    public PageInfo<SelectPoorApplyVo> selectPoorApplyList(SelectPoorApplyDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getLimit());
        List<SelectPoorApplyPo> poorApplyList = applyMapper.getPoorApplyList(dto);
        List<SelectPoorApplyVo> poorApplyVoList = new ArrayList<>();
        for (SelectPoorApplyPo applyPo : poorApplyList){
            SelectPoorApplyVo applyVo = new SelectPoorApplyVo();
            BeanUtils.copyBeanProp(applyVo,applyPo);
            String poorGrade = PoorGradeTran(applyPo.getResultRank());
            applyVo.setResultRank(poorGrade);
            applyVo.setType(applyPo.getType());
            String examine = ExamineTran(applyPo.getStatus());
            applyVo.setStatus(examine);
            poorApplyVoList.add(applyVo);
        }
        PageInfo<SelectPoorApplyVo> pageInfo = new PageInfo<SelectPoorApplyVo>(poorApplyVoList);
        return pageInfo;
    }


    /**
     * 贫困等级装换
     * @param code Integer 值
     * @return 字符串
     */
    public String PoorGradeTran(Integer code){
        if(code == null){
            return null;
        }
        if (code.equals(PoorGrade.NO_POOR.getCode())){
            return PoorGrade.NO_POOR.getMessage();
        }else if(code.equals(PoorGrade.COMMONLY_POOR.getCode())){
            return PoorGrade.COMMONLY_POOR.getMessage();
        }else {
            return PoorGrade.SPECIAL_POOR.getMessage();
        }
    }

    /**
     * 贫困类型装换
     * @param code Integer 值
     * @return 字符串
     */
    public String poorTypeTran(Integer code){
        if (code.equals(PoorType.DISEASE.getCode())){
            return PoorType.DISEASE.getMessage();
        }else if(code.equals(PoorType.SUBSISTENCE_ALLOWANCE.getCode())){
            return PoorType.SUBSISTENCE_ALLOWANCE.getMessage();
        }else {
            return PoorType.DISABILITY.getMessage();
        }
    }

    /**
     * 审核状态
     * @param code Integer 值
     * @return 字符串
     */
    public String ExamineTran(Integer code){
        if (code.equals(ExamineStatus.SUCCESS.getCode())){
            return ExamineStatus.SUCCESS.getMessage();
        }else if(code.equals(ExamineStatus.FAILED.getCode())){
            return ExamineStatus.FAILED.getMessage();
        }else {
            return ExamineStatus.EXAMINE.getMessage();
        }

    }

}
